package com.fa4j.common.web.util;


import com.fa4j.common.web.model.AppType;
import com.fa4j.common.web.model.ClientInfo;
import com.fa4j.common.web.model.CommonWebConst;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.util.ObjectUtils;

import static org.springframework.http.HttpHeaders.USER_AGENT;

public class ClientUtil {

    public static ClientInfo getClientInfo(HttpServletRequest request) {
        ClientInfo clientInfo = new ClientInfo();
        clientInfo.setClientAppVersion(request.getHeader(CommonWebConst.HEADER_APP_VER));
        String appType = request.getHeader(CommonWebConst.HEADER_APP_TYPE);
        if (!ObjectUtils.isEmpty(appType)) {
            clientInfo.setClientAppType(AppType.valueOf(appType));
        }
        clientInfo.setClientIp(IpUtil.getRemoteIp());
        clientInfo.setClientUserAgent(request.getHeader(USER_AGENT));
        clientInfo.setClientAppEnv(request.getHeader(CommonWebConst.HEADER_APP_ENV));
        return clientInfo;
    }
}
